home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Mac OS X Throbber / DialogWatcher / RSO.cdev.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-24  |  3.9 KB  |  120 lines

  1. #define SystemSevenOrLater 1
  2.  
  3. #include <Events.h>
  4. #include <Resources.h>
  5. #include <Memory.h>
  6. #include <Devices.h>
  7. #include <Dialogs.h>
  8. #include <OSUtils.h>
  9. #include <ToolUtils.h>
  10. #include <Gestalt.h>
  11.  
  12. #include "RSO.h"
  13.  
  14. // dialog items for our control panel:
  15.  
  16. enum {
  17.     kJivingOn = 1,
  18.     kJivingOff = 2,
  19.     kJivText = 3
  20. };
  21.  
  22. JiverGlobals    *gInitGlobals, **gPrefsResource;
  23.  
  24. ControlHandle    gOnButton, gOffButton;
  25. Handle            gTextBox;
  26.  
  27. #ifdef powerc
  28.         // for Metrowerks' linker, this defines the mixed-mode interface for main().
  29.     ProcInfoType __procinfo = kPascalStackBased
  30.              | RESULT_SIZE(kFourByteCode)
  31.              | STACK_ROUTINE_PARAMETER(1,  SIZE_CODE(sizeof(short)))
  32.              | STACK_ROUTINE_PARAMETER(2,  SIZE_CODE(sizeof(short)))
  33.              | STACK_ROUTINE_PARAMETER(3,  SIZE_CODE(sizeof(short)))
  34.              | STACK_ROUTINE_PARAMETER(4,  SIZE_CODE(sizeof(short)))
  35.              | STACK_ROUTINE_PARAMETER(5,  SIZE_CODE(sizeof(EventRecord *)))
  36.              | STACK_ROUTINE_PARAMETER(6,  SIZE_CODE(sizeof(long)))
  37.              | STACK_ROUTINE_PARAMETER(7,  SIZE_CODE(sizeof(DialogPtr)));
  38.  
  39. #endif
  40.  
  41. pascal long main(short message, short Item, short numItems, short CPanelID,
  42.                  EventRecord *theEvent, register long cdevValue, DialogPtr CPDialog)
  43. {
  44.     long    oldA4;
  45.     OSErr    err;
  46.     short    kind;
  47.     Rect    r;
  48.     Str255    text;
  49.     SelectorFunctionUPP    ourGestaltUPP;
  50.     long    versionCheck;
  51.  
  52.     oldA4 = SetCurrentA4();
  53.  
  54.     if (message == initDev) {
  55.         gPrefsResource = (JiverGlobals **)Get1Resource('pref', -4048);
  56.         if (!gPrefsResource) {
  57.                 // if we can't load our preferences resource, something is seriously wrong.
  58.     bad_problem:
  59.             cdevValue = cdevGenErr;
  60.         } else {
  61.             HNoPurge((Handle) gPrefsResource);
  62.  
  63.                 // make sure that our prefs resource is the correct size.
  64.             SetHandleSize((Handle) gPrefsResource, sizeof(JiverGlobals));
  65.             if (GetHandleSize((Handle) gPrefsResource) != sizeof(JiverGlobals)) goto bad_problem;
  66.  
  67.             err = Gestalt(kDTS_Signature, (long *)&ourGestaltUPP);
  68.             if (err == noErr) {
  69.                 err = CallSelectorFunctionProc(ourGestaltUPP, gestaltVersion, (long *)&versionCheck);
  70.                 if (versionCheck != 0x0100) err = -1;
  71.                 if (err == noErr) {
  72.                     err = CallSelectorFunctionProc(ourGestaltUPP, kGestaltGetInitGlobals, (long *)&gInitGlobals);
  73.                 }
  74.             }
  75.             if (err != noErr) {
  76.                 gInitGlobals = 0;
  77.             } else {
  78.                     // if our INIT portion loaded, make sure our prefs resource matches its settings.
  79.                 **gPrefsResource = *gInitGlobals;
  80.             }
  81.             GetDialogItem(CPDialog, numItems + kJivingOn,  &kind, (Handle *) &gOnButton,  &r);
  82.             GetDialogItem(CPDialog, numItems + kJivingOff, &kind, (Handle *) &gOffButton, &r);
  83.             GetDialogItem(CPDialog, numItems + kJivText,   &kind,            &gTextBox,   &r);
  84.             BlockMove((**gPrefsResource).JivString, text, sizeof((**gPrefsResource).JivString));
  85.             SetDialogItemText(gTextBox, text);
  86.             SelectDialogItemText(CPDialog, numItems + kJivText, 0, text[0]);
  87.             cdevValue = 237;    // any positive number should do.
  88.             goto updateButtons;
  89.         }
  90.     } else if (cdevValue == 237) { // handle non-initDev messages only if initialization succeeded.
  91.         switch (message) {
  92.             case closeDev:
  93.                 ChangedResource((Handle) gPrefsResource);
  94.                 break;
  95.             case hitDev:
  96.                 Item -= numItems;
  97.                 if (Item == kJivingOn || Item == kJivingOff) {
  98.                     (**gPrefsResource).JiverOn = (Item == kJivingOn);
  99.     updateButtons:    SetControlValue(gOnButton,   (**gPrefsResource).JiverOn);
  100.                     SetControlValue(gOffButton, !(**gPrefsResource).JiverOn);
  101.                 }
  102.                 break;
  103.             case nulDev:
  104.                 // for some stupid reason, we never get notified when our user types new text
  105.                 // into our text area.  So we have to wait until null events come by, and
  106.                 // check the area at that time.  Here, all we do is 
  107.                 GetDialogItemText(gTextBox, text);
  108.                 if (text[0] > 31) text[0] = 31;
  109.                 BlockMove(text, (**gPrefsResource).JivString, sizeof((**gPrefsResource).JivString));
  110.                 if (gInitGlobals) {
  111.                     *gInitGlobals = **gPrefsResource;
  112.                 }
  113.                 break;
  114.         } // switch (message)
  115.     }
  116.  
  117.     SetA4(oldA4);
  118.     return cdevValue;
  119. }
  120.